package com.example.dict.ativities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dict.R;
import com.example.dict.bean.WordBean;
import com.example.dict.db.DBManager;
import com.example.dict.utils.ApiUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class WordInfoActivity extends BaseActivity {


    TextView mZi,mPy,mBs,mBh,mWb,mJsTv,mXXJsTv;
    ListView mJsLv;
    ImageView mCollectIv;
    //标志位，表示该汉字是否被收藏
    boolean isCollect = false;
    boolean isExist = false;//判断汉字是否已经存在

    private String zi;
    List<String> mDatas; //数据源
    private ArrayAdapter<String> adapter;
    private  List<String> jijie;
    private List<String> mXiangjie;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_info);
        //1、接收上一个页面接收过来的对象
        Intent intent = getIntent();
         zi = intent.getStringExtra("zi");
        String Url = ApiUtil.getWordurl(zi); //拼接网址
        initView();
        //初始化listView的数据源
        mDatas = new ArrayList<>();
        adapter = new ArrayAdapter<String>(this, R.layout.item_word_jslv, R.id.item_wordlv_tv, mDatas);
        mJsLv.setAdapter(adapter);
        //加载网络数据
        loadData(Url);
        //判断是否已经收藏的方法
        isExist = DBManager.isExistInCOLLWORD(zi);
        isCollect = isExist;//记录初始状态
        setCollectTvStyle();
    }
    /*根据收藏的状态，改变图片颜色*/
    private void setCollectTvStyle() {
        if (isCollect) {
            mCollectIv.setImageResource(R.mipmap.ic_collection_fs);
        }else {
            mCollectIv.setImageResource(R.mipmap.ic_collection);
        }
    }

    /**2、
     * 数据获取成功时调用的方法
     * @param result
     */
    @Override
    public void onSuccess(String result) {
        Gson gson = new Gson();
        WordBean wordBean = gson.fromJson(result, WordBean.class);
        WordBean.ResultBean resultBean = wordBean.getResult();
        //插入数据库、
        DBManager.insertWordTOWord(resultBean);
        //将数据显示在View上
        notifyView(resultBean);
    }

    /**
     * 更新控件信息
     * @param resultBean
     */
    private void notifyView(WordBean.ResultBean resultBean) {
        mZi.setText(resultBean.getZi());
        mPy.setText(resultBean.getPinyin());
        mBs.setText("部首："+resultBean.getBushou());
        mBh.setText("笔画："+resultBean.getBihua());
        mWb.setText("五笔："+resultBean.getWubi());
        jijie = resultBean.getJijie();
        mXiangjie = resultBean.getXiangjie();
        //默认一进去就显示基本释义
            mDatas.clear();
            mDatas.addAll(jijie);
            adapter.notifyDataSetChanged();

    }

    /**
     * 数据获取失败时调用的方法
     * @param ex
     * @param isOnCallback
     */
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        //联网失败，后获取数据库信息
        WordBean.ResultBean bean = DBManager.queryWordFromWord(zi);
        if (bean !=null) {
            notifyView(bean);//更新
        }
    }

    private void initView() {

        mZi = findViewById(R.id.word_tv_zi);
        mPy = findViewById(R.id.word_tv_pinyin);
        mBs = findViewById(R.id.word_tv_bushou);
        mBh = findViewById(R.id.word_tv_bihua);
        mWb = findViewById(R.id.word_tv_wubi);
        mJsTv = findViewById(R.id.word_tv_bjs);
        mXXJsTv = findViewById(R.id.word_tv_djs);
        mJsLv = findViewById(R.id.word_layout_jslv);
        mCollectIv = findViewById(R.id.wordInfo_iv_collection);
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wordInfo_iv_back:
                finish();
                break;
            case R.id.wordInfo_iv_collection:
                isCollect = !isCollect;
                setCollectTvStyle();
                break;
            case R.id.word_tv_bjs:
                mJsTv.setTextColor(Color.RED);
                mXXJsTv.setTextColor(Color.BLACK);
                //清空之前的数据源

                    mDatas.clear();
                    mDatas.addAll(jijie);
                    adapter.notifyDataSetChanged();
                break;
            case R.id.word_tv_djs:
                mXXJsTv.setTextColor(Color.RED);
                mJsTv.setTextColor(Color.BLACK);
                //清空之前的数据源

                    mDatas.clear();
                    mDatas.addAll(mXiangjie);
                    adapter.notifyDataSetChanged();
                break;
        }
    }

    /*activity销毁时调用的方法
    * 将汉字插入或删除数据*/
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isExist && !isCollect) {
            //原来收藏了但是现在不想收藏了
            DBManager.deleteZiFromCOLLWORD(zi);
        }
        if (!isExist && isCollect) {
            //原因不收藏，现在要收藏
            DBManager.insertZiToCOLLWORD(zi);
        }
    }
}
